<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const formLabelWidth = '160px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    list:[] as Array<any>,
  })
  interface formQuert {
    id:number
    user_id:number
    minutes:number | string
    nums:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    user_id: [{ required: true, trigger: 'blur', message: '请输入user_id' }],
    minutes: [{ required: true, trigger: 'blur', message: '请输入系统回收间隔' }],
    nums: [{ required: true, trigger: 'blur', message: '请输入增加金豆数' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { id } = form
      if(!id) {
        await equipApi.addSystemUser(form).then(res=>{
          submitSet = true
          ToastSuccess('新增系统回收用户成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(id) {
        await equipApi.addSystemUserJD(form).then(res=>{
          submitSet = true
          ToastSuccess('系统回收用户金豆数新增成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
      
    })
  }
  const openDialog = async(query:any = {})=> {
    const {
      id
    } = query
    if(!id) {
      action.title = '新增系统回收用户'
    }
    if(id) {
      action.title = '新增系统回收用户金豆'
      Object.assign(form,{
        id
      })
    }
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="55%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <template v-if="!form.id">
          <el-form-item :label-width="formLabelWidth" label="user_id" prop="user_id">
            <el-input v-model="form.user_id" placeholder="请输入user_id" />
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="系统回收间隔(分钟)" prop="minutes">
            <el-input v-model="form.minutes" placeholder="请输入系统回收间隔(分钟)" />
          </el-form-item>
        </template>
        <el-form-item v-if="form.id" :label-width="formLabelWidth" label="	增加金豆数" prop="nums">
          <el-input v-model="form.nums" placeholder="请输入增加金豆数" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>